<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-09-29
 * Time: 13:48
 */

namespace backend\controllers;


use app\models\CtiCallRecord;
use app\models\CtiFile;
use app\models\CtiGw;
use common\helps\Tools;
use common\helps\UploadFiles;
use moonland\phpexcel\Excel;
use yii\filters\VerbFilter;

class CtiManagerGwController  extends CtiManagerController
{

    /**
     * 导入网关文件里面的内容
     */
    public function actionUpload()
    {
        try{
            $model = new CtiFile();
            $file = $_FILES['file'];
            if (!empty($file)) {
                $count = CtiFile::find()->select("*")->where(["name"=>$file['name']])->count();
                $model->name = $file['name'];
                if ($count>0){//表面所上传的文件已经上传过了，
                    return "1";
                }
                $myfile = UploadFiles::common_upload_single($file);
                if(!empty($model->name) && $myfile){
                    //开启事务
                    $connection = \Yii::$app->getDb();
                    $transaction = $connection->beginTransaction();
                    //先保存表；
                    $getExel = Excel::import([
                        'file'=>$myfile['filepath'],
                    ]);
                    $model->type = "cti_gw";
                    $model->path = $this->https_url.$myfile['filename'];
                    $model->filesize = $file['size'];
                    $model->create_at = time();
                    $model->save();
                    $array_key = [
                        '所属地市' => "city",
                        '码号状态' => "status",
                        '所属号段' => "belong",
                        '*号码' => "bind_number",
                        '鉴权密码' => "password",
                        '新增人' => "name",
                        '新增时间' => "create_time",
                        '修改时间' => "update_time",
                        '修改人' => "update_person",
                        '号码类型' => "num_type",
                        '激活状态' => "enable",
                        '关联端口' => "port",
                    ];
                    $models = $model->find()->where('1=1')->orderBy('id desc')->one();
                    $id = $models->id;
                    foreach($getExel['file'] as $attributes)
                    {
                        foreach($attributes as $k=>$v)
                        {
                            $attributes_new[$array_key[$k]] = "".$v;
                        }
                        $CtiGw = new CtiGw();
                        $CtiGw->file_id = $id;
                        $CtiGw->create_at = time();
                        $CtiGw->setAttributes($attributes_new);
                        $CtiGw->number = substr($attributes_new['bind_number'],strlen($attributes_new['bind_number'])-4,strlen($attributes_new['bind_number']));
                        $CtiGw->save();
                    }
                    //更新导入了几条记录
                    $model->updateAttributes(['number'=>count($getExel['file'])]);
                    $transaction->commit();
                    //事务完成，一次性提交
                }
                return "true";
            }else{
                return "false";
            }
        }catch (\Exception $e){
            Tools::l($e);
        }
    }

    /**
     * 更新坐席的数据
     */
    public function actionUpdate(){
        if (isset($_POST['id'])){
            $id = $_POST['id'];
            $number = $_POST['number'];
            $password = $_POST['password'];
            $bind_number = $_POST['bind_number'];
            $Gw = CtiGw::findOne($id);
            $res = $Gw->updateAttributes(['number'=>$number,'password'=>$password,'bind_number'=>$bind_number]);
        }else{
            $post = $_POST;
            $post['name'] = \Yii::$app->getUser()->getIdentity()->username;
            $post['create_at'] = time();
            $Gw = new CtiGw();
            $Gw->load(['CtiGw'=>$post]);
            $res = $Gw->save();
        }
        return $res;
    }

    /**
     * 删除坐席的数据
     */
    public function actionDelete(){
        $id = $_POST['id'];
        $model = CtiGw::findOne($id);
        $res = $model->delete();
        return $res;
    }

    /**
     * 坐席的电话号码的列表
     */
    public function actionList()
    {
        return $this->getList(new CtiGw());
    }

    /**
     * 将坐席的数据导出到本地，格式为exel/csv
     * @param unknown $id
     */
    public function actionTemplateDownload()
    {
        try{
            $model = new CtiGw();
            Excel::export([
                'models'=>$model,
                'fileName'=>time().'_gw_template',
                'columns'=>[
                    '1',
                    '2',
                    '3',
                    '4',
                    '5',
                    '6',
                    '7',
                    '8',
                    '9',
                    '10',
                    '11',
                    '12',
                ],
                'headers'=>[
                    '1'=>'所属地市',
                    '2'=>'码号状态',
                    '3'=>'所属号段',
                    '4'=>'*号码',
                    '5'=>'鉴权密码',
                    '6'=>'新增人',
                    '7'=>'新增时间',
                    '8'=>'修改时间',
                    '9'=>'修改人',
                    '10'=>'号码类型',
                    '11'=>'激活状态',
                    '12'=>'关联端口',
                ]
            ]);
        }catch (\Exception $exception){
            Tools::l($exception);
        }

    }

    /**
     * 文件列表
     */
    public function actionFileList()
    {
        return $this->getFileList(new CtiFile(),"cti_gw");
    }

}